
def test_translate_abs():
    """
    一次性翻译data目录下的所有pkl文件
    """
    from abs_translate.get_abs import DumpJson,show_abs
    import os
    for file_name in os.listdir("data/"):
        entry_id = file_name.split(".pkl")[0]
        DumpJson(entry_id)
    show_abs()

def test_tag_system():
    from meta_service.tag import ArxivTag,InitTag
    from meta_service.store import ArxivPaper
    import os
    for file_name in os.listdir("data/"):
        entry_id = file_name.split(".pkl")[0]
        paper = ArxivPaper(entry_id)
        tag = InitTag(paper)
        tag.AddTag("test","test00234")
        tag.AddTag("id",str(paper.get_entry_id()))
        print(tag)
        print("==========\n")
 
def test_researcher():
    """
    用于从arxiv获取文献资料
    """
    from app.researcher import AppRun,Date,Today
    # BeginDate = Date(2024,9,1)
    # EndDate = Today()
    AppRun(
        query=f"(ti:hallucination OR abs:hallucination) AND cat:cs.AI AND submittedDate:[{Date(2024,9,1)} TO {Today()}]",
        # query=f"(ti:intermediate OR abs:intermediate) AND cat:cs.AI AND submittedDate:[{BeginDate} TO {EndDate}]",
        # query=f"(ti:patch OR abs:patch) AND cat:cs.AI AND submittedDate:[{BeginDate} TO {EndDate}]",
        max_results=1000
    )

def test_tagabs():
    """
    生成年月日的Tag
    """
    from app.tagabs import GetOrInitTagForAllPkl
    GetOrInitTagForAllPkl("data/")

def test_llm():
    from extra_service.llm.conversation import Conversation,Req,Content,ContentText,ContentImage
    c = Conversation()
    content = Content("user",[
        ContentText("下面图片中有什么？请用中文回答"),
        ContentImage("123.png"),
        # ContentImage("123.png"),
    ])
    c.Add(content)

    from extra_service.llm import LLMClient
    client = LLMClient("127.0.0.1:7002")
    output,c = client.Predict(c)
    print(output)
    print(c)
    # print(c.to_dict())
    # Req(c)

def test_download_pdf():
    from meta_service.store import ArxivPaper
    paper = ArxivPaper("2311.13314v1")
    paper.download_to_dir("output/pdf_dump/")

def test_selected_keyw():
    """
    从全局的entry_id列表中,筛选出符合Tag的文献
    """
    from app.search_keyw.keyw import ListSelectedTag
    # SelectByKeyWords(["decode","decoding"])
    # ListSelectedKeyw(["decode","decoding"])
    ListSelectedTag("user_select","select")

def test_singlepdf():
    """
    下载特定的arxiv文献,并使用kimi生成对应的markdown问答
    """
    from app.singlepdf import SinglePdfRun
    from app.singlepdf.pline import title_list,pt_list

    import app.singlepdf
    app.singlepdf.single.GLOBAL_PDF_URL = "https://arxiv.org/pdf/2409.03271"
    app.singlepdf.single.GLOBAL_TITLE_LIST = title_list
    app.singlepdf.single.GLOBAL_PROMPT_LIST = pt_list

    out_file_path = "./output.md"
    history = SinglePdfRun(out_file_path)
    print(history)

from extra_service.openreview import *
def test_openreview_singlepdf(paper):
    """
    使用kimi生成对应的markdown问答
    输出到: arxiv_singlepdf
    """
    from meta_service.tistore import MAIN_TIS
    from app.singlepdf import SinglePdfRun
    from app.singlepdf.pline import title_list,pt_list

    # if MAIN_TIS.HasTopic("openreview.iclr.2025"):
    #     handle = MAIN_TIS.GetTopicHandle("openreview.iclr.2025")
    #     paper = LoadPaperFromTisHandleAtIdx(handle,paper_idx)
    if not isinstance(paper,OpenReviewPaper):
        raise Exception("paper is not OpenReviewPaper")

    import app.singlepdf
    app.singlepdf.single.GLOBAL_PDF_URL = str(paper.get_pdf_url())
    app.singlepdf.single.GLOBAL_TITLE_LIST = title_list
    app.singlepdf.single.GLOBAL_PROMPT_LIST = pt_list

    # tis = TopicIndexStore("./tis_main_store")
    topic_handle = MAIN_TIS.EnsureTopicHandle("app.singlepdf")
    # topic_handle = tis.EnsureTopicHandle("app.singlepdf")

    from meta_service.utils import format_title
    title = format_title(paper.get_title())
    index_name = f"{title}.md"

    if topic_handle.HasIndex(index_name):
        print(f"repeat index: {index_name}, title: {title}")
        return
    index_handle = topic_handle.NewTempIndex(index_name)
    out_file_path = index_handle.GetIndexPath()

    history = SinglePdfRun(out_file_path)
    index_handle.TurnOn()
    print(history)

def test_arxiv_singlepdf(id):
    """
    下载特定的arxiv文献,并使用kimi生成对应的markdown问答
    输出到: arxiv_singlepdf
    """
    from app.singlepdf import SinglePdfRun
    from app.singlepdf.pline import title_list,pt_list
    from meta_service.store import ArxivPaper

    # id = "2403.04696"
    from app.researcher import SearchId
    id = SearchId(id)

    paper = ArxivPaper(id)

    import app.singlepdf
    app.singlepdf.single.GLOBAL_PDF_URL = str(paper.get_pdf_url())
    app.singlepdf.single.GLOBAL_TITLE_LIST = title_list
    app.singlepdf.single.GLOBAL_PROMPT_LIST = pt_list

    from meta_service.tistore import TopicIndexStore

    tis = TopicIndexStore("./tis_main_store")
    topic_handle = tis.EnsureTopicHandle("app.singlepdf")

    from meta_service.utils import format_title
    title = format_title(paper.get_title())
    index_name = f"{title}.md"

    if topic_handle.HasIndex(index_name):
        print(f"repeat index: {index_name}, entry-id: {paper.get_entry_id()}")
        return
    index_handle = topic_handle.NewTempIndex(index_name)
    out_file_path = index_handle.GetIndexPath()

    history = SinglePdfRun(out_file_path)
    index_handle.TurnOn()
    # print(history)

def test_searchone():
    """
    搜索并下载特定arxiv-id的文献
    """
    from app.researcher import SearchId
    real_id = SearchId("2403.07556")
    print(real_id)

def test_list_selected_papers():
    """
    从指定的entry_id列表中,筛选出符合Tag的文献
    """
    from app.search_keyw.keyw import ListSelectedTagInPapers
    from meta_service.store import ArxivPaper
    replaced_entry_id_list = ['2409.06485v1', '2408.17150v1', '2408.16589v1', '2408.12326v1', '2408.11261v1', '2408.02032v1', '2407.15130v2', '2407.07071v1', '2407.06426v1', '2406.17642v1', '2406.12221v1', '2406.05183v1', '2406.00069v1', '2405.19519v1', '2405.18654v1', '2405.17821v1', '2405.17820v1', '2405.15356v1', '2405.06545v1', '2405.04600v3', '2404.10198v2', '2404.09480v1', '2404.03865v1', '2404.01588v1', '2403.19094v2', '2403.16167v3', '2403.14003v1', '2403.09037v2', '2403.01548v3', '2403.00425v2', '2402.18476v1', '2402.17097v2', '2402.15300v2', '2402.14545v2', '2402.11875v1', '2401.02132v1']
    out = ListSelectedTagInPapers(replaced_entry_id_list,"user_select","select")
    # for entry_id in out:
    #     paper = ArxivPaper(entry_id)
    #     print(f"https://arxiv.org/abs/{entry_id}")
    #     print("Title:",paper.get_title())
    # print(len(out))
    print(out)


def test_pipe_search_in_2024papers():
    """
    利用流水线从2024年的arxiv文献中筛选出符合条件的文献
    筛选范围是 title + abs_summary

    您可以将筛选得到的列表, 放到 view_service 中手动筛选
    然后再利用 ListSelectedTagInPapers 收集自己手动筛选的文献列表
    """
    # 每个 keys 中只要有一个关键词出现在文本中,就认为是符合条件的
    keys1 = [ "hallucination", "hallucinate", "hallucinat" ]
    keys2 = [ "token-level", "token level" , "token" ]
    keys3 = [ "mitigating" , "mitigate" , "mitigation" ]
    keys4 = [ "fine-grained hallucinations" ] # 细粒度幻觉
    # 筛选流
    T = [
        keys1,
        keys2,
        keys3
    ]
    from app.pipe_search import PipeSearchInYear2024Papers
    out = PipeSearchInYear2024Papers(T)

def test_batch_singlepdf():
    batch_single_pdf_list = ['2408.11261v1', '2403.01548v3', '2406.17642v1', '2408.12326v1', '2406.05183v1', '2406.00069v1', '2405.06545v1', '2405.18654v1', '2402.18476v1', '2407.15130v2', '2408.02032v1', '2405.17821v1', '2402.14545v2', '2404.09480v1', '2407.07071v1', '2403.00425v2', '2403.14003v1', '2402.17097v2', '2405.15356v1', '2403.09037v2', '2405.17820v1', '2403.16167v3', '2401.02132v1', '2402.15300v2', '2406.12221v1', '2404.01588v1', '2402.11875v1', '2408.17150v1', '2409.06485v1']
    import time
    for entry_id in batch_single_pdf_list:
        print(f"{entry_id} begin...")
        test_arxiv_singlepdf(entry_id)
        print(f"{entry_id} is done")
        time.sleep(30)

def test_tag_all_meeting():
    """
    通过识别comment的内容,检索paper的会议
    """
    from app.auto_comment import CheckMeetingInComment
    from meta_service.store import GetAllEntryIds
    from meta_service.tag import ArxivTag
    all = GetAllEntryIds()
    for entry_id in all:
        print(entry_id)
        CheckMeetingInComment(entry_id)
        tager = ArxivTag(entry_id)
        out = tager.GetTag("Meeting")
        print(f"{entry_id} > {out}")
        # break

def test_init_tis_store():
    from meta_service.tistore import MAIN_TIS
    # # topic_list = MAIN_TIS.GetTopicList()
    # # print(topic_list)
    # handle = None
    # if MAIN_TIS.HasTopic("openreview.iclr.2025"):
    #     handle = MAIN_TIS.GetTopicHandle("openreview.iclr.2025")
    # index_list = handle.GetIndexList()
    # print(index_list)

from extra_service.openreview import *
from meta_service.utils import *
import pickle
def test_load_paper_from_tis():
    from meta_service.tistore import MAIN_TIS
    MAIN_TIS.EnsureTopicHandle("rematch.download.all")
    dtopic = MAIN_TIS.GetTopicHandle("rematch.download.all")
    if MAIN_TIS.HasTopic("rematch"):
        handle = MAIN_TIS.GetTopicHandle("rematch")
        # count = CountPaperInTisHandle(handle)
        # print(count)
        papers = LoadPaperFromTisHandle(handle,-1)
        print(f"load ok! Count: {len(papers)}")
        papers = Filter(papers).filter(get_filter_func_RatingUpTo(7)).filter(get_filter_func_KeyWordsMatch("hallucinat")).dump()
        with open("outss","wb") as fp:
            pickle.dump(papers,fp)
        # papers:list[OpenReviewPaper] = LoadPaperFromTisHandle(handle,10)
        print("download...")
        for idx,paper in enumerate(papers):
            try:
                print(paper.get_title())
                index_name = format_title(paper.get_title()) + ".pdf"
                index_handle = dtopic.NewTempIndex(index_name)
                file_path = index_handle.GetIndexPath()
                download_pdf(
                    paper.get_pdf_url(),
                    os.path.dirname(file_path),
                    os.path.basename(file_path)
                )
                if get_file_size(file_path) > 0:
                    index_handle.TurnOn()
            except Exception as e:
                print(f"idx: {idx} is error: {str(e)}")
                continue
        return papers

        
def test_check():
    import time
    papers = test_load_paper_from_tis()
    for idx,paper in enumerate(papers):
        print(f"idx: {idx}")
        print(paper.get_title())
        test_openreview_singlepdf(paper)
        print("ok")
        time.sleep(20)


def test_lancer():
    from meta_service.ucentre import AnyLancer,AnyTag
    from meta_service.tag import ArxivTag,GetArxivTag
    paper = AnyLancer("2406.00069v1")
    # tag = ArxivTag("2406.00069v1")
    tag = GetArxivTag(paper)
    print(paper.get_title())
    print(tag.GetTagDict())

test_lancer()
# test_tag_all_meeting()
# test_batch_singlepdf()
# test_pipe_search_in_2024papers()
# test_list_selected_papers()
# test_arxiv_singlepdf("2406.00069v1")
# test_tag_system()
# test_researcher()
# test_tagabs()
# test_init_tis_store()
# test_load_paper_from_tis()
# test_llm()
# test_download_pdf()
# test_selected_keyw()
# test_singlepdf()
# test_searchone()
# test_openreview_singlepdf(0)
# test_check()
